/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
// 时空 o(n) o(1)
// 找到链表中点 将后面一半翻过来
// 一个指针指向链表头部 另一个指针指向翻转的那一半的头部
// 二者同时遍历到有一个出现为null，中间如果出现不相等就返回false
// 否则返回true
var isPalindrome = function(head) {
  if(head.next === null) return true
  let slow = head, fast = head
  while(slow && fast.next) {
    slow = slow.next
    fast = fast.next.next
  }
  let ptr = null
  while(slow) {
    let next = slow.next
    slow.next = ptr
    ptr = slow
    slow = next
  }
  slow = head
  while(ptr && slow) {
    if(ptr.val !== slow.val) return false
    ptr = ptr.next
    slow = slow.next
  }
  return true
};